﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoxObject : SlotObject
{
    // Start is called before the first frame update
    protected new void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public override bool Move(int dx, int dy, float duration = 0.3f) {
        base.Move(dx, dy, duration);
        slot.SlotState = Slot.State.box;
        return true;
    }

    public override bool OnPlayerTouch(Player player, int dx = 0, int dy = 0) {
        Slot newSlot = slot.GetNewSlot(dx, dy);
        if(newSlot == null) {
            return false;
        }
        else if(newSlot.SlotState == Slot.State.empty) {
            Move(dx, dy, 0.3f);
            return true;
        }
        else if(newSlot.SlotState == Slot.State.none) {
            Move(dx, dy, 0.3f);
            StartCoroutine(SetEmptyCoroutine(0.3f));
            return true;
        }
        return false;
    }

    IEnumerator SetEmptyCoroutine(float duration) {
        yield return new WaitForSeconds(duration);
        slot.gridManager.EliminateTeris();
        slot.SetEmpty();
    }
}
